#include "dictpack.h"

IMPLEMENT_APP_CONSOLE( DictPackConsole )


int DictPackConsole::rmindex() {
    wxPrintf(_T("Removing .daodict indexes ..."));
    
    p_dictdb_utils = new DictDBUtils(daodict_path);
    int ret = p_dictdb_utils->rm_indexes();
    
    wxPrintf(_T("\tOK!\n"));
    
    delete p_dictdb_utils;
    
    return ret;
}

int DictPackConsole::pack() {
    rmindex();
    
    daopack_path = daodict_path;
    daopack_path.Replace(_T(".daodict"), _T(".daopack"));
    
    wxFFileInputStream daodict_strm(daodict_path);
    wxFFileOutputStream daopack_strm(daopack_path);
    wxZlibOutputStream pack_strm(daopack_strm, wxZ_BEST_COMPRESSION, wxZLIB_GZIP);

    wxPrintf(_T("Compressing file %s to %s ..."), daodict_path.c_str(), daopack_path.c_str());
    
    daopack_strm.Write(DAOPACK_FULL_MAGIC_NUM, DAOPACK_FULL_MAGIC_NUM_LEN);
    pack_strm.Write(daodict_strm);
    
    wxPrintf(_T("\tOK!\n"));
    wxPrintf(_T("Finished!\n"));
}

int DictPackConsole::mkindex() {
    wxPrintf(_T("Creating .daodict indexes ..."));
    
    p_dictdb_utils = new DictDBUtils(daodict_path);
    int ret = p_dictdb_utils->mk_indexes();
    
    wxPrintf(_T("\tOK!\n"));
    
    delete p_dictdb_utils;
    return ret;
}

int DictPackConsole::unpack() {
    daodict_path = daopack_path;
    daodict_path.Replace(_T(".daopack"), _T(".daodict"));
    
    wxFFileInputStream daopack_strm(daopack_path);
    wxZlibInputStream pack_strm(daopack_strm);
    wxFFileOutputStream daodict_strm(daodict_path);

    wxPrintf(_T("Uncompressing file %s to %s ..."), daopack_path.c_str(), daodict_path.c_str());
    
    char full_magic_num[DAOPACK_FULL_MAGIC_NUM_LEN] = {0};
    daopack_strm.Read(full_magic_num, DAOPACK_FULL_MAGIC_NUM_LEN);
    daodict_strm.Write(pack_strm);
    
    wxPrintf(_T("\tOK!\n"));
    
    daodict_strm.Close();
    mkindex();
    
    wxPrintf(_T("Finished!\n"));
}

void DictPackConsole::OnInitCmdLine(wxCmdLineParser& parser) {
    parser.SetSwitchChars(_T("-"));
    
    wxString logo = _T("dictadmin ");
    logo= logo + _T(PRODUCT_VERSION_NUMBER "\n" PRODUCT_COPYRIGHT) + 
            _T("\n\nAdministration daodict dictionaries.\n");
    parser.SetLogo(logo);

    parser.AddOption(_T("u"), _T("unpack"), 
            _T("unpack .daodict (dictionary file) from .daopack file"), wxCMD_LINE_VAL_STRING);

    parser.AddOption(_T("p"), _T("pack"), 
            _T("pack .daodict (dictionary file) to .daopack file"), wxCMD_LINE_VAL_STRING);
    
    parser.AddOption(_T("d"), _T("output-dir"), 
            _T("specify output directory"), wxCMD_LINE_VAL_STRING);
    
    return wxAppConsole::OnInitCmdLine(parser);
}

bool DictPackConsole::OnCmdLineParsed(wxCmdLineParser& parser) {
    bool ret_p = parser.Found(_T("p"),&daodict_path);
    bool ret_u = parser.Found(_T("u"),&daopack_path);
    parser.Found(_T("d"),&output_dir);

    if (false==(ret_p||ret_u)) { // print help
        wxAppConsole::OnCmdLineHelp(parser);
    }
    if (true==(ret_p&&ret_u)) { // print help
        wxPrintf(_T("DO NOT use '-p' and '-u' at the same time.\n"));
        return false;
    }
    
    return wxAppConsole::OnCmdLineParsed(parser);
}

int DictPackConsole::OnRun() {
    if (!daodict_path.IsEmpty()) {
        return pack();
    }
    
    if (!daopack_path.IsEmpty()) {
        return unpack();
    }
    
    return 1;
}
